import threading
from qkd_keygen_script import QKDKeyGen

class QKDLink:
    def __init__(self, master_address, slave_address, delay, cost):
        self.master_address = master_address  # Address of the master node (Alice)
        self.slave_address = slave_address  # Address of the slave node (Bob)
        self.delay = delay  # Network delay in milliseconds
        self.cost = cost  # Cost of using the link
        self.key_gen = None  # QKDKeyGen instance, to be initialized later

    def initialize_qkd_keygen(self, key_size, key_rate, packet_size, data_rate, max_sifting_packets):
        # Initialize QKDKeyGen as a component of QKDLink
        self.key_gen = QKDKeyGen(
            key_size=key_size,
            key_rate=key_rate,
            packet_size=packet_size,
            data_rate=data_rate,
            max_sifting_packets=max_sifting_packets,
            master_address=self.master_address,
            slave_address=self.slave_address
        )
        # Start the key generation process in a separate thread
        threading.Thread(target=self.key_gen.start_communication, daemon=True).start()

    def get_link_info(self):
        # Return basic information about the link
        return {
            "master_address": self.master_address,
            "slave_address": self.slave_address,
            "delay": self.delay,
            "cost": self.cost
        }

# Example usage
# if __name__ == "__main__":
#     link = QKDLink(
#         master_address="192.168.1.1",
#         slave_address="192.168.1.2",
#         delay=10,
#         cost=5
#     )
#     link.initialize_qkd_keygen(
#         key_size=256,
#         key_rate=5,
#         packet_size=1024,
#         data_rate=100,
#         max_sifting_packets=10
#     )
